//
//  ViewController.m
//  Random
//
//  Created by 宜凯门-DKang on 2017/5/18.
//  Copyright © 2017年 ucanmax. All rights reserved.
//

#import "ViewController.h"
#import "MBProgressHUD.h"
#import "AllMemberViewController.h"

#define Key @"random"
#define PATH NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]
@interface ViewController ()<UITextFieldDelegate>
@property (nonatomic,assign)BOOL isStart;
@property (nonatomic,retain)NSString *nameStr;
@property (nonatomic,retain)NSTimer *timer;
@property (nonatomic,retain)MBProgressHUD *hud;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isStart = NO;
    self.textField.delegate = self;
    UITapGestureRecognizer *tapGesturRecognizer=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    [self.nameView addGestureRecognizer:tapGesturRecognizer];
    
    self.timer= [NSTimer scheduledTimerWithTimeInterval:0.0001 target:self selector:@selector(random) userInfo:nil repeats:YES];
    [self endRandom];
}

- (void)showText:(NSString *)text {
    if(!self.hud){
        self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    }
    self.hud.mode = MBProgressHUDModeText;
    self.hud.label.text = text;
    // Move to bottm center.
    self.hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
    
    [self.hud hideAnimated:YES afterDelay:2.f];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:YES];
    [self endRandom];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)action:(id)sender {
    [self.textField resignFirstResponder];
    if([self getArray].count < 1){
        return;
    }
    [self.actionButton setTitle:self.isStart == YES ? @"START":@"END" forState:UIControlStateNormal];
    if(self.isStart == YES){
        [self endRandom];
    }else {
        [self startRandom];
    }
    self.isStart = !self.isStart;
}

- (void)tapAction:(id)action {
    [self.textField resignFirstResponder];
    if([self getArray].count < 1){
        return;
    }
    [self.actionButton setTitle:self.isStart == YES ? @"START":@"END" forState:UIControlStateNormal];
    if(self.isStart == YES){
        [self endRandom];
    }else {
        [self startRandom];
    }
    self.isStart = !self.isStart;
}
- (void)startRandom {
    NSArray *arr = [self getArray];
    if(arr.count > 0){
         [self.timer setFireDate:[NSDate date]];
    }else {
        [self showText:@"请添加成员"];
    }
}
- (void)endRandom {
     [self.timer setFireDate:[NSDate distantFuture]];
}

- (void)random {
    NSArray *arr = [self getArray];
    if(arr.count > 0){
        int x = arc4random() % arr.count;
        self.nameLabel.text = arr[x];
    }else {
        [self endRandom];
    }
    
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
    self.nameStr = textField.text;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    self.nameStr = textField.text;
    [self.textField resignFirstResponder];
    return YES;
}
- (IBAction)add:(id)sender {
    [self.textField resignFirstResponder];
    self.nameStr = self.textField.text;
    if(self.nameStr.length){
        NSMutableArray* arr = [self getArray];
        NSMutableArray *array = [[NSMutableArray alloc]initWithArray:arr];
        [array addObject:self.nameStr];
        
        BOOL isWrite = [array writeToFile:[self getFilePath] atomically:YES];
        if(isWrite == YES){
            self.textField.text = nil;
            [self showText:@"添加成功"];
        }
    }else {
        [self showText:@"请输入内容"];
    }
    
}
- (NSString *)getFilePath {
    return [PATH stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",Key]];
}
- (NSMutableArray *)getArray {
    NSString *filePath = [PATH stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",Key]];
    NSMutableArray *arr = [[NSMutableArray alloc]initWithContentsOfFile:filePath];
    return arr;
}
- (IBAction)lookAll:(id)sender {
    NSMutableArray* arr = [self getArray];
    if(arr.count > 0){
        AllMemberViewController *vc = [[AllMemberViewController alloc]initWithNibName:@"AllMemberViewController" bundle:[NSBundle mainBundle]];
        if(vc){
            [self.navigationController pushViewController:vc animated:YES];
        }
    }else {
        [self showText:@"请添加成员"];
    }
}


@end
